package team.seekers.skill.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import team.seekers.skill.common.utils.ParamCheckUtils;
import team.seekers.skill.entity.dao.SectionBlog;
import team.seekers.skill.entity.vo.admin.add.AdminBlogAddVO;
import team.seekers.skill.entity.vo.admin.update.AdminBlogUpdateVO;
import team.seekers.skill.result.Result;
import team.seekers.skill.result.ResultCode;
import team.seekers.skill.service.SectionBlogService;

import java.util.List;


@RestController
@RequestMapping("/api/skill/blog")
@Validated
public class SectionBlogController {

    @Autowired
    private SectionBlogService sectionBlogService;

    @PostMapping("/add")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result add(@RequestBody AdminBlogAddVO adminBlogAddVO){
        if(ParamCheckUtils.isNotRightId(adminBlogAddVO.getSectionId())){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        SectionBlog sectionBlog = new SectionBlog();
        BeanUtils.copyProperties(adminBlogAddVO,sectionBlog);
        boolean save = sectionBlogService.save(sectionBlog);
        if(!save){
            return Result.fail(ResultCode.ACTION_FAIL);
        }
        return Result.success();
    }

    @DeleteMapping("/delete/{blogId}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result delete(@PathVariable("blogId") String blogId){
        if(ParamCheckUtils.isNotRightId(blogId)){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        boolean remove = sectionBlogService.removeById(blogId);
        if(!remove){
            return Result.fail(ResultCode.ACTION_FAIL);
        }
        return Result.success();
    }

    @PutMapping("/update")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result update(@RequestBody AdminBlogUpdateVO adminBlogUpdateVO){
        if(ParamCheckUtils.isNotRightId(adminBlogUpdateVO.getId())){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        SectionBlog sectionBlog = sectionBlogService.getById(adminBlogUpdateVO.getId());
        BeanUtils.copyProperties(adminBlogUpdateVO,sectionBlog);
        boolean updateById = sectionBlogService.updateById(sectionBlog);
        if(!updateById){
            return Result.fail(ResultCode.ACTION_FAIL);
        }
        return Result.success();
    }

    @GetMapping("/listUpdateVOBySectionId/{sectionId}")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Result listBySectionId(@PathVariable("sectionId") String sectionId){
        if(ParamCheckUtils.isNotRightId(sectionId)){
            return Result.fail(ResultCode.PARAM_NOT_VALID);
        }
        List<AdminBlogUpdateVO> adminBlogUpdateVOList = sectionBlogService.listAdminBlogUpdateVOBySectionId(sectionId);
        if(adminBlogUpdateVOList == null){
            return Result.fail(ResultCode.GET_FAIL);
        }
        return Result.success("rows",adminBlogUpdateVOList);
    }

}
